package org.example;

import java.io.IOException;
import java.net.*;

public class SendCode {


    private SocketAddress socketAddress;//发送IP端口设置

    byte[] data;//发送的数据编码


    SendCode(String serverIp) throws IOException, InterruptedException {

        //1.创建数据
        byte[] data = new byte[1024];
        data[0] = (byte)0x01;
        data[1] = (byte)0x03;
        data[2] = (byte)0x10;
        data[3] = (byte)0x00;
        data[4] = (byte)0x00;
        data[5] = (byte)0x0A;
        data[6] = (byte)0xF0;
        data[7] = (byte)0x7B;
//        System.out.println(data);

        //待发送数据
        String str ="01 03 10 00 00 0A C1 0D";
//        String str ="01 03 10 00 00 0A F0 7B";
        data= hexStringToByteArray(str);

        socketAddress = new InetSocketAddress(serverIp,608);        //发送到指定IP端口的数据包属性初始化

        DatagramPacket packet = new DatagramPacket(data, data.length,socketAddress);//创建数据报，包含发送的数据信息


        /*
        *
        * 本机发送端口设置,因为多次设置同一个ip监听会报错，不可以重复设置同一个端口IP进行监听
        *
        * */
        SocketAddress clienSocket;
        clienSocket = new InetSocketAddress("192.168.100.246",608);
        DatagramSocket socket = new DatagramSocket(clienSocket);

        int time=10000;
        while (time>0){
            socket.send(packet);// 4.向服务器端发送数据报
            System.out.println(time);


            // 创建一个缓冲区,容量尽量设置大一点因为不知道发送过来的信息有多大
            byte[] recevieByte = new byte[25];//创建字节数组(此处接收的数据格式为固定长度,所以创建了固定大小的字节数组)
            // 接收数据报
            DatagramPacket receivePacket = new DatagramPacket(recevieByte, recevieByte.length,clienSocket);
            // 接收数据
            socket.receive(receivePacket);
//            // 获取接收端发送过来的真实长度以及数据
//            byte[] data = receivePacket.getData();
//            int length = receivePacket.getLength();
//            // 获取发送者的IP
//            InetAddress address1 = receivePacket.getAddress();
//            String ip = address1.getHostAddress();
//            System.out.println("接收来自:"+ip+"的数据，内容是:"+reply);



            Thread.sleep(1000);//休眠1秒
            time--;
        }


    }






    //string to byte[]
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    //byte[] to string
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }


}
